import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter_app/data_model.dart';
import 'package:http/http.dart' as http;

class FutureIcan extends StatefulWidget {
  const FutureIcan({super.key});

  @override
  State<FutureIcan> createState() => _FutureIcanState();
}

class _FutureIcanState extends State<FutureIcan> {
  @override
  Widget build(BuildContext context) {
    return FutureBuilder<ResponseModel>(
        future: fetchGet(),
        builder: (BuildContext context, AsyncSnapshot<ResponseModel> snapshot){
            switch(snapshot.connectionState){
              case ConnectionState.none: return Text('state none');
              case ConnectionState.active: return Text('active');
              case ConnectionState.waiting: return Center(child: CircularProgressIndicator(),);
              case ConnectionState.done:
                if(snapshot.hasError){
                  return Text('error');
                }else{
                  return Column(children: [Text('code ${snapshot.data?.code}'),Text('${snapshot.data?.data.list}')],);
                }
            }
        });
  }

  Future<ResponseModel>fetchGet() async{
    var uri = Uri.parse(
      'http://127.0.0.1:4523/m2/5951398-0-default/266599765',
    );
    var res = await http.get(uri);
    // resultShow = res.body;
    var map = jsonDecode(res.body);
    var resModal = ResponseModel.fromJson(map);
    return resModal;
  }
}
